package dao.imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import model.Member;

import dao.DaoHelper;
import dao.MemberDao;

public class MemberDaoImp implements MemberDao {

	private static MemberDaoImp memberDao = new MemberDaoImp();
	private static DaoHelper daoHelper=DaoHelperImp.getBaseDaoInstance();

	private MemberDaoImp(){}

	public static MemberDaoImp getInstance()
	{
		return memberDao;
	}

	@Override
	public boolean save(Member member) {
		// TODO Auto-generated method stub
		Connection con=daoHelper.getConnection();
		PreparedStatement stmt=null;
		boolean success = true;

		try {	
			stmt=con.prepareStatement("insert into member value(?, ?, ?, ?, ?, ?, ?, ?)");
			stmt.setString(1, member.getName());
			stmt.setString(2, member.getPassword());
			stmt.setString(3, member.getGender());
			stmt.setInt(4, member.getAge());
			stmt.setString(5, member.getAddress());
			stmt.setFloat(6, member.getBalance());
			stmt.setTimestamp(7, member.getLastRecharge());
			stmt.setFloat(8, member.getTotalConsumption());
			stmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			success = false;
		} finally {
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
		}
		
		return success;
		
	}

	@Override
	public Member find(String name, String password) {
		// TODO Auto-generated method stub
		Connection con=daoHelper.getConnection();
		PreparedStatement stmt=null;
		ResultSet result=null;
		String n = null;
		String p = null;
		String g = null;
		int a = 0;
		String add = null;
		float b = -1;
		Timestamp l = null;
		float t = 0;
		Member member = null;

		try {	
			stmt=con.prepareStatement("select * from member where name = ? and password = ?");
			stmt.setString(1, name);
			stmt.setString(2, password);
			result=stmt.executeQuery();
			if (result.next()) {
				n = result.getString(1);
				p = result.getString(2);
				g = result.getString(3);
				a = result.getInt(4);
				add = result.getString(5);
				b = result.getFloat(6);
				l = result.getTimestamp(7);
				t = result.getFloat(8);
				member = new Member(n, p, g, a, add, b, l, t);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
			daoHelper.closeResult(result);
		}

		return member;
		
	}

	@Override
	public boolean updateMessageByName(Member member) {
		// TODO Auto-generated method stub
		Connection con=daoHelper.getConnection();
		PreparedStatement stmt=null;
		boolean success = true;

		try {	
			stmt=con.prepareStatement("update member set password = ?, gender = ?, age = ?, address = ? where name = ?");
			stmt.setString(1, member.getPassword());
			stmt.setString(2, member.getGender());
			stmt.setInt(3, member.getAge());
			stmt.setString(4, member.getAddress());
			stmt.setString(5, member.getName());
			stmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			success = false;
		} finally {
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
		}
		
		return success;
		
	}
	
	public boolean updateBalanceByName(Member member) {
		// TODO Auto-generated method stub
		Connection con=daoHelper.getConnection();
		PreparedStatement stmt=null;
		boolean success = true;

		try {	
			stmt=con.prepareStatement("update member set balance = ?, lastRecharge = ? where name = ?");
			stmt.setFloat(1, member.getBalance());
			stmt.setTimestamp(2, member.getLastRecharge());
			stmt.setString(3, member.getName());
			stmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			success = false;
		} finally {
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
		}
		
		return success;
		
	}
	
	@Override
	public boolean updateConsumptionByName(Member member) {
		// TODO Auto-generated method stub
		Connection con=daoHelper.getConnection();
		PreparedStatement stmt=null;
		boolean success = true;

		try {	
			stmt=con.prepareStatement("update member set totalConsumption = ? where name = ?");
			stmt.setFloat(1, member.getTotalConsumption());
			stmt.setString(2, member.getName());
			stmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			success = false;
		} finally {
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
		}
		
		return success;
	}

	@Override
	public List<Member> findAll() {
		// TODO Auto-generated method stub
		Connection con=daoHelper.getConnection();
		PreparedStatement stmt=null;
		ResultSet result=null;
		String n = null;
		String p = null;
		String g = null;
		int a = 0;
		String add = null;
		float b = -1;
		Timestamp l = null;
		float t = 0;
		Member member = null;
		ArrayList<Member> list = new ArrayList<Member>();

		try {	
			stmt=con.prepareStatement("select * from member");
			result=stmt.executeQuery();
			while (result.next()) {
				n = result.getString(1);
				p = result.getString(2);
				g = result.getString(3);
				a = result.getInt(4);
				add = result.getString(5);
				b = result.getFloat(6);
				l = result.getTimestamp(7);
				t = result.getFloat(8);
				member = new Member(n, p, g, a, add, b, l, t);
				list.add(member);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
			daoHelper.closeResult(result);
		}

		return list;
		
	}

	@Override
	public Member find(String name) {
		// TODO Auto-generated method stub
		Connection con=daoHelper.getConnection();
		PreparedStatement stmt=null;
		ResultSet result=null;
		String n = null;
		String p = null;
		String g = null;
		int a = 0;
		String add = null;
		float b = -1;
		Timestamp l = null;
		float t = 0;
		Member member = null;

		try {	
			stmt=con.prepareStatement("select * from member where name = ?");
			stmt.setString(1, name);
			result=stmt.executeQuery();
			if (result.next()) {
				n = result.getString(1);
				p = result.getString(2);
				g = result.getString(3);
				a = result.getInt(4);
				add = result.getString(5);
				b = result.getFloat(6);
				l = result.getTimestamp(7);
				t = result.getFloat(8);
				member = new Member(n, p, g, a, add, b, l, t);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
			daoHelper.closeResult(result);
		}

		return member;
		
	}

	@Override
	public boolean removeByName(String name) {
		// TODO Auto-generated method stub
		Connection con=daoHelper.getConnection();
		PreparedStatement stmt=null;
		boolean success = true;

		try {	
			stmt=con.prepareStatement("delete from member where name = ?");
			stmt.setString(1, name);
			stmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			success = false;
		} finally {
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
		}
		
		return success;
		
	}

}
